package org.yuanqiframework.yuanqi.core.utils.captcha;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.core.codec.Base64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 图片验证码
 */
public class ImageCaptchaUtils {
    private static final String CAPTCHA_PREFIX = "CAPTCHA_IMG:";
    public static final int default_width = 260;
    public static final int default_height = 80;
    public static final int default_codeCount = 4;
    public static final int default_thickness = 4;
    private static final Long CAPTCHA_EXPIRE_TIME = 120L; //2分钟
    private static final ConcurrentHashMap<String, String> CAPTCHA_MAP = new ConcurrentHashMap<>();

    /**
     * 生成验证码
     *
     * @param width
     * @param height
     * @param codeCount
     * @param thickness
     * @return
     * @throws IOException
     */
    public static ImageCaptcha captcha(int width, int height, int codeCount, int thickness) throws IOException {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(width, height, codeCount, thickness);
        String code = captcha.getCode();
        RenderedImage image = (RenderedImage) captcha.createImage(code);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", byteArrayOutputStream);
        String key = CAPTCHA_PREFIX + UUID.randomUUID().toString().replace("-", "");
        //验证码图片
        String base64Src = "data:image/jpg;base64," + Base64Encoder.encode(byteArrayOutputStream.toByteArray());
        HashMap<String, Object> map = new HashMap<>();
        return new ImageCaptcha(key, code, base64Src);
    }

    /**
     * 默认参数
     *
     * @return
     * @throws IOException
     */
    public static ImageCaptcha captcha() throws IOException {
        return captcha(default_width, default_height, default_codeCount, default_thickness);
    }

    /**
     * 内存存储
     *
     * @return
     * @throws IOException
     */
    public static void cacheInMemory(ImageCaptcha imageCaptcha, Long timeout) {
        String key = imageCaptcha.getKey();
        //内存实现
        CAPTCHA_MAP.put(key, imageCaptcha.getCode());
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                CAPTCHA_MAP.remove(key);
            }
        }, timeout == null ? CAPTCHA_EXPIRE_TIME * 1000 : timeout * 1000);
    }

    public static void cacheInMemory(ImageCaptcha imageCaptcha) {
        cacheInMemory(imageCaptcha, CAPTCHA_EXPIRE_TIME);
    }


    public static Boolean testInMemory(String key, String code) {
        if (key == null || key.isEmpty() || code == null || code.isEmpty()) {
            return false;
        }
        return CAPTCHA_MAP.containsKey(key) && code.equals(CAPTCHA_MAP.get(key));
    }


}
